#include <iostream>
#include <stack>

struct data { int t[2], d; };

std::istream & operator>>(std::istream & is, data & a)
{
	char c1, c2;
	is >> c1 >> c2; a.t[0] = ((c1 - '0') * 10 + (c2 - '0')) * 60;
	is >> c1;
	is >> c1 >> c2; a.t[0] += (c1 - '0') * 10 + (c2 - '0');

	is >> c1 >> c2; a.t[1] = ((c1 - '0') * 10 + (c2 - '0')) * 60;
	is >> c1;
	is >> c1 >> c2; a.t[1] += (c1 - '0') * 10 + (c2 - '0');
	is >> a.d;
	return is;
}

int const maxn = 1001;
int head[2 * maxn], end_point[4 * maxn], next[4 * maxn];
int alloc = 2;
data da[maxn];
int n;

void add_edge(int u, int v)
{
	end_point[alloc] = v; next[alloc] = head[u]; head[u] = alloc++;
}

int low[maxn], dfn[maxn], color[maxn];
bool instack[maxn];
int tick, color_tick;
std::stack<int> stk;

void tarjan(int u, int parent)
{
	low[u] = dfn[u] = ++tick;
	stk.push(u);
	instack[u] = true;
	for (int p = head[u]; p; p = next[p]) {
		int v = end_point[p];
		if (v == parent) continue;
		if (!dfn[v]) {
			tarjan(v, u);
			low[u] = std::min(low[u], low[v]);
		} else if (instack[v])
			low[u] = std::min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u]) {
		int t;
		++color_tick;
		do { t = stk.top(); stk.pop(); color[t] = color_tick; }
		while (t != u);
	}
}

bool conflict(data const & a, int i1, data const & b, int i2)
{
	if (a.t[i1] + a.d <= b.t[i2]) return false;
	if (b.t[i2] + b.d <= a.t[i1]) return false;
	return true;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	for (int i = 1; i <= n; i++) {
		std::cin >> da[i];
		add_edge(i, 3 * n + i);
		add_edge(n + i, 2 * n + i);
	}
	for (int i = 1; i <= n; i++)
		for (int j = i + 1; j <= n; j++) {
			if (conflict(da[i], 0, da[j], 0)) {
				add_edge(i, 2 * n + j);
				add_edge(j, 2 * n + i);
			}
			if (conflict(da[i], 1, da[j], 1)) {
				add_edge(n + i, 3 * n + j);
				add_edge(n + j, 3 * n + i);
			}
			if (conflict(da[i], 0, da[j], 1)) {
				add_edge(i, 3 * n + j);
				add_edge(n + j, i);
			}
			if (conflict(da[i], 1, da[j], 0)) {
				add_edge(n + i, 2 * n + j);
				add_edge(j, 3 * n + i);
			}
		}
	for (int i = 1; i <= 4 * n; i++) if (!dfn[i]) tarjan(i, -1);
}

